# Maintainer: spider-mario <spidermario@free.fr>
# Contributor: Taras Shpot <mrshpot@gmail.com>
pkgname=('rust-git' 'rust-doc-git')
pkgver=1.0.0.alpha.471.g3bf41da
epoch=2
pkgrel=1
pkgdesc="A safe, concurrent, practical language from Mozilla."
arch=('i686' 'x86_64')
url="http://www.rust-lang.org/"
license=('custom:MIT' 'Apache')
makedepends=('git' 'gcc' 'curl'
             'libffi' 'python2')
optdepends=('haskell-pandoc: to build the documentation'
            'emacs: to build the emacs mode')
source=("git+https://github.com/mozilla/rust.git"
        "git+https://github.com/rust-lang/compiler-rt.git"
        "git+https://github.com/rust-lang/jemalloc.git"
        "git+https://github.com/rust-lang/llvm.git"
        "git+https://github.com/rust-lang/hoedown.git")
noextract=(compiler-rt jemalloc llvm hoedown)
sha512sums=(SKIP SKIP SKIP SKIP SKIP)

pkgver() {
	cd rust
	git describe | sed -e 's/^release-//' -e 'y/-/./'
}

prepare() {
	cd rust
  #fixed: this doesn't do its job: git submodules are cloned again even though pacman (makepkg) already got them in sources=  above
  echo "$startdir"
  echo "$srcdir"
  echo "$SRCDEST"
#  set|grep -i sources
#  exit 1
	_startdir="$SRCDEST" perl -pi -- /dev/stdin .gitmodules <<'END'
		if (m{(?<assignment> \s* url \s* = \s*) .* / (?<submodule> [^/]*?)(?:\.git)?$}x) {
			my $local = "$ENV{_startdir}/$+{submodule}";
			$_ = "$+{assignment}$local\n" if -d $local;
		}
END
}

build() {
	cd rust
  echo "$srcdir"
#  exit 1

	./configure --prefix=/usr --disable-rpath --disable-verify-install --disable-debug --enable-manage-submodules --enable-clang --enable-ccache --enable-dist-host-only --disable-libcpp
  #XXX: --disable-libcpp to fix 'cstddef' file not found https://github.com/rust-lang/rust/issues/14023#issuecomment-70646368
  #--llvm-root="${SRCDEST}/llvm" --jemalloc-root="${srcdir}/jemalloc"
  #--llvm-root="${srcdir}/rust/src/llvm" --jemalloc-root="${srcdir}/rust/src/jemalloc"
  #--llvm-root="${srcdir}/llvm" --jemalloc-root="${srcdir}/jemalloc"
	make -j4 all
}

package_rust-git() {
	depends=('shared-mime-info')
	optdepends=('rust-doc-git: language and API documentation')
	provides=('rust')
	conflicts=('rust')
	options=('staticlibs')
	install=rust.install

	cd rust

	make DESTDIR="$pkgdir" install

	install --directory "$pkgdir/usr/share/licenses/rust-git/"
	install -m644 COPYRIGHT LICENSE-* "$pkgdir/usr/share/licenses/rust-git/"

	install --directory "$pkgdir/usr/share/apps/katepart/syntax/"
	cp src/etc/kate/rust.xml "$pkgdir/usr/share/apps/katepart/syntax/"

	cp -a src/etc/gedit/share "$pkgdir/usr/"

	install --directory "$pkgdir/usr/share/vim/"
	cp -a src/etc/vim "$pkgdir/usr/share/vim/vimfiles"

	install --directory "$pkgdir/usr/share/zsh/functions/Completion/Zsh/"
	cp -a src/etc/zsh/* "$pkgdir/usr/share/zsh/functions/Completion/Zsh/"

	cd src/etc/emacs
	emacs --eval '(byte-recompile-directory "." 0)' --quick --batch 2> /dev/null || true
	install --directory "$pkgdir/usr/share/emacs/site-lisp/"
	cp -a * "$pkgdir/usr/share/emacs/site-lisp/"
}

package_rust-doc-git() {
	pkgdesc="A safe, concurrent, practical language from Mozilla. (Language and API documentation)"
	arch=('any')
	options+=('!strip')
	optdepends=('rust-git: to compile and run the programs you can write using this documentation')
	provides=('rust-doc')
	conflicts=('rust-doc')

	cd rust

	_docdir="$pkgdir/usr/share/doc/rust"
	install --directory "$_docdir"
	cp -r doc/* "$_docdir/" || true

	chmod -R 644 "$_docdir"
	find "$_docdir" -type d -exec chmod 755 {} +
	for ext in aux out log toc; do
		rm -f "$_docdir"/*."$ext"
	done
}
